package ipc

import (
	"encoding/json"
)

type IpcClient struct {
	conn chan string
}

func NewIpcClient(server *IpcServer) *IpcClient {
	conn := server.Connect()

	return &IpcClient{conn}
}

func (client *IpcClient) Call(method, params string) (resp *Response, err error) {

	var b []byte

	req := &Request{method, params}

	b, err = json.Marshal(req)

	if err != nil {
		return
	}

	client.conn <- string(b)

	str := <-client.conn //等待返回结果

	var resp1 Response

	err = json.Unmarshal([]byte(str), &resp1)

	resp = &resp1

	return

}

func (client *IpcClient) Close() {

	client.conn <- "CLOSE"

}
